package com.har.modules.web.controller.resource;

import com.alibaba.fastjson.JSONObject;
import com.har.common.exception.RRException;
import com.har.common.utils.AESUtil;
import com.har.common.utils.R;
import com.har.modules.web.entity.CompanyEntity;
import com.har.modules.web.entity.UserEntity;
import com.har.modules.web.service.CompanyService;
import com.har.modules.web.service.UserService;
import com.har.modules.web.service.WeChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 微信相关接口
 * @author maoruidd
 * @create 2018 - 08 - 03 - 上午 10:29
 */
@Slf4j
@RestController
@RequestMapping("/wechat")
public class WeChatResoure {

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private UserService userService;

    @Autowired
    private CompanyService companyService;



    /**
     *  绑定微信接口
     * @return
     */
    @RequestMapping("/qrcode")
    public R userAuthorize(HttpServletRequest request) throws Exception {
        Cookie [] cookies=request.getCookies();
        String data=null;
        JSONObject respJson=new JSONObject();
        if(null!=cookies){
            for (Cookie cookie:cookies){
                if(cookie.getName().equals("login")){
                    data = AESUtil.getInstance().decrypt(cookie.getValue());
                    respJson = JSONObject.parseObject(data);
                }
            }
//            respJson.put("type",1);
//            respJson.put("id",5);
            if(data==null){
                throw new RRException("请先登录或者注册");
            }
            return R.ok().put("data",weChatService.userAuthorize(respJson));
        }else {
            throw new RRException("请先登录或者注册");
        }
//return R.ok().put("data",weChatService.userAuthorize(respJson));
    }

    /**
     * 微信回调接口
     * @param request
     * @param code
     * @param state
     * @return
     */
    @RequestMapping("/callBack")
    public String callBack(HttpServletRequest request,String code,String state) throws Exception {

        JSONObject dataJson=weChatService.callBack(code,state);
        log.info("wechat callback result>>>>{}",dataJson.toJSONString());
        //截取用户类型  1:企业 0:员工
        Integer type=Integer.parseInt(state.substring(0,1));
        if(1==type){
            companyService.updateNickName(dataJson);
        }
        if(0==type){
            userService.updateNickName(dataJson);
        }
        return  "ok";
    }


    /**
     * 微信昵称查询接口
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    public R info(HttpServletRequest request) throws Exception {
        Cookie [] cookies=request.getCookies();
        String data=null;
        JSONObject respJson=new JSONObject();
        if(null!=cookies){
            for (Cookie cookie:cookies){
                if(cookie.getName().equals("login")){
                    data = AESUtil.getInstance().decrypt(cookie.getValue());
                    respJson = JSONObject.parseObject(data);
                }
            }
            if(data==null){
                throw new RRException("请先登录或者注册");
            }
            Integer type=Integer.parseInt(respJson.getString("type"));
            Long id=Long.valueOf(respJson.getString("id"));
            if(1==type){
                CompanyEntity com=companyService.queryNickName(id);
                return R.ok().put("data",com);
            }
            if(0==type){
                UserEntity user=userService.queryNickName(id);
                return R.ok().put("data",user);
            }
        }
        return R.ok();
    }

}
